//
//  TabViewController.m
//  AnimationTabBar
//
//  Created by I三生有幸I on 15/6/6.
//  Copyright (c) 2015年 盛辰. All rights reserved.
//

#import "TabViewController.h"
#import "RootViewController.h"
#import "DetailViewController.h"
#import "ThirdViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"

#define KScreenHeight [[UIScreen mainScreen]bounds].size.height
#define KScreenWidth [[UIScreen mainScreen]bounds].size.width
@interface TabViewController () <UITabBarControllerDelegate>

// 装载三个动画button的view
@property(nonatomic, retain)UIView *myView;

// tabBar中间的button
@property(nonatomic, retain)UIButton *centerButton;

// 三个动画button
@property(nonatomic, retain)UIButton *buttonOne;
@property(nonatomic, retain)UIButton *buttonTwo;
@property(nonatomic, retain)UIButton *buttonThree;
@property(nonatomic, retain)RootViewController *rootViewVc;

// 是否显示三个动画button
@property(nonatomic, assign)BOOL isShow;
@end

@implementation TabViewController
- (void)dealloc
{
    [_myView release];
    [_buttonOne release];
    [_buttonTwo release];
    [_buttonThree release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
//    // 注册通知
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonAction:) name:@"HiddenButton" object:nil];
    
    
    // 自己是tabBarController 继承UITabBarController
    self.delegate = self;
    
    // 创建五个导航控制器和与其对应的根视图
    self.rootViewVc = [[RootViewController alloc]init];
    UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:_rootViewVc];
    self.rootViewVc.tabBarItem.title = @"第一个";
    self.rootViewVc.tabBarItem.image = [UIImage imageNamed:@"53-house.png"];
    
    DetailViewController *detailViewVc = [[DetailViewController alloc]init];
    UINavigationController *detailNav = [[UINavigationController alloc]initWithRootViewController:detailViewVc];
    detailViewVc.tabBarItem.title = @"第二个";
    detailViewVc.tabBarItem.image = [UIImage imageNamed:@"53-house.png"];
    
    ThirdViewController *thirdViewVc = [[ThirdViewController alloc]init];
    UINavigationController *thirdNav = [[UINavigationController alloc]initWithRootViewController:thirdViewVc];
    thirdViewVc.tabBarItem.title = @"第三个";
    thirdViewVc.tabBarItem.image = [UIImage imageNamed:@"53-house.png"];
    
    FourViewController *fourViewVc = [[FourViewController alloc]init];
    UINavigationController *fourNav = [[UINavigationController alloc]initWithRootViewController:fourViewVc];
    fourViewVc.tabBarItem.title = @"第四个";
    fourViewVc.tabBarItem.image = [UIImage imageNamed:@"53-house.png"];
    
    FiveViewController *fiveViewVc = [[FiveViewController alloc]init];
    UINavigationController *fiveNav = [[UINavigationController alloc]initWithRootViewController:fiveViewVc];
    fiveViewVc.tabBarItem.title = @"第五个";
    fiveViewVc.tabBarItem.image = [UIImage imageNamed:@"53-house.png"];
    
    // 装载五个导航控制器
    self.viewControllers = @[rootNav, detailNav, thirdNav, fourNav, fiveNav];
    
    [_rootViewVc release];
    [rootNav release];
    [detailViewVc release];
    [detailNav release];
    [thirdViewVc release];
    [thirdNav release];
    [fourViewVc release];
    [fourNav release];
    [fiveViewVc release];
    [fiveNav release];
    
    // 加载动画视图
    [self setAnimationView];
    
    
}


#pragma mark - 加载动画视图 -

- (void)setAnimationView
{
    
    
    // 创建view 在tabBar下方 此时看不见
    // 装载三个动画button的view
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(90, KScreenHeight, 200, 50)];
   // self.myView.backgroundColor = [UIColor redColor];
    self.myView.alpha = 0;
    [self.view addSubview:_myView];
    [_myView release];
    
    // tabBar中间button
    self.centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.centerButton.frame = CGRectMake((KScreenWidth - 60) / 2, -10, 60, 60);
    self.centerButton.tag = 100;
    [self.centerButton setImage:[UIImage imageNamed:@"28b58PICHyt.jpg"] forState:UIControlStateNormal];
    self.centerButton.layer.masksToBounds = YES;
    [self.centerButton.layer setCornerRadius:30];
    [self.centerButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBar addSubview:_centerButton];
    
    
    // 三个动画button
    self.buttonOne = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOne.frame = CGRectMake(70, 0, 60, 60);
    [self.buttonOne setImage:[UIImage imageNamed:@"28b58PICHyt.jpg"] forState:UIControlStateNormal];
    self.buttonOne.layer.masksToBounds = YES;
    [self.buttonOne.layer setCornerRadius:30];
    [self.buttonOne addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:_buttonOne];
    
    self.buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonTwo.frame = CGRectMake(70, 0, 60, 60);
    [self.buttonTwo setImage:[UIImage imageNamed:@"28b58PICHyt.jpg"] forState:UIControlStateNormal];
    self.buttonTwo.layer.masksToBounds = YES;
    [self.buttonTwo.layer setCornerRadius:30];
    [self.buttonTwo addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:_buttonTwo];
    
    self.buttonThree = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonThree.frame = CGRectMake(70, 0, 60, 60);
    [self.buttonThree setImage:[UIImage imageNamed:@"28b58PICHyt.jpg"] forState:UIControlStateNormal];
    self.buttonThree.layer.masksToBounds = YES;
    [self.buttonThree.layer setCornerRadius:30];
    [self.buttonThree addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:_buttonThree];
}

// button触发方法
- (void)buttonAction:(UIButton *)button
{
    CABasicAnimation* rotationAnimation3;
    rotationAnimation3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation3.toValue = [NSNumber numberWithFloat: M_PI * 5.0 ];
    rotationAnimation3.duration = 1;
    rotationAnimation3.repeatCount = 1;
    [self.rootViewVc.tableView.layer addAnimation:rotationAnimation3 forKey:nil];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDelegate:self];
    self.rootViewVc.tableView.frame = CGRectMake((375 - 100) / 2, 0, 100, 0);
    self.rootViewVc.tableView.alpha = 0;
    [UIView commitAnimations];
    self.rootViewVc.isShow = NO;
    
    [UIView animateWithDuration:1 animations:^{
        
        if (_isShow == NO)
        {
            CGRect viewframe = self.myView.frame;
            viewframe.origin.y = KScreenHeight - 120;
            self.myView.frame = viewframe;
            self.myView.alpha = 1;
            
            CGRect buttonOneFrame = self.buttonOne.frame;
            buttonOneFrame.origin.x = 0;
            self.buttonOne.frame = buttonOneFrame;
            
            CGRect buttonTwoFrame = self.buttonTwo.frame;
            buttonTwoFrame.origin.x = 70;
            self.buttonTwo.frame = buttonTwoFrame;
            
            CGRect buttonThreeFrame = self.buttonThree.frame;
            buttonThreeFrame.origin.x = 140;
            self.buttonThree.frame = buttonThreeFrame;

            
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonAction:) name:@"HiddenButton" object:nil];

            _isShow = YES;

        }
        else
        {
            CGRect viewframe = self.myView.frame;
            viewframe.origin.y = KScreenHeight;
            self.myView.frame = viewframe;
            self.myView.alpha = 0;
            
            CGRect buttonOneFrame = self.buttonOne.frame;
            buttonOneFrame.origin.x = 70;
            self.buttonOne.frame = buttonOneFrame;
            
            CGRect buttonTwoFrame = self.buttonTwo.frame;
            buttonTwoFrame.origin.x = 70;
            self.buttonTwo.frame = buttonTwoFrame;
            
            CGRect buttonThreeFrame = self.buttonThree.frame;
            buttonThreeFrame.origin.x = 70;
            self.buttonThree.frame = buttonThreeFrame;

            [[NSNotificationCenter defaultCenter]removeObserver:self name:@"HiddenButton" object:nil];
            
            _isShow = NO;

        }
    } completion:nil];
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 5.0 ];
    rotationAnimation.duration = 1;
    rotationAnimation.repeatCount = 1;
    
    [self.centerButton.layer addAnimation:rotationAnimation forKey:nil];
    [self.buttonOne.layer addAnimation:rotationAnimation forKey:nil];
    [self.buttonTwo.layer addAnimation:rotationAnimation forKey:nil];
    [self.buttonThree.layer addAnimation:rotationAnimation forKey:nil];
}


#pragma mark - tabBarController点击代理方法 -

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"点击了tabBarItem");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
